home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2001 December / pcwk12201b.iso / Wersje pelne i specjalne / Winamp 2.77 i 3.0beta / wasabi-sdk_beta1.exe / studio / common / ptrlist.cpp < prev    next >
C/C++ Source or Header  |  2001-10-08  |  5KB  |  178 lines

  1. /*
  2.  
  3.   Nullsoft WASABI Source File License
  4.  
  5.   Copyright 1999-2001 Nullsoft, Inc.
  6.  
  7.     This software is provided 'as-is', without any express or implied
  8.     warranty.  In no event will the authors be held liable for any damages
  9.     arising from the use of this software.
  10.  
  11.     Permission is granted to anyone to use this software for any purpose,
  12.     including commercial applications, and to alter it and redistribute it
  13.     freely, subject to the following restrictions:
  14.  
  15.     1. The origin of this software must not be misrepresented; you must not
  16.        claim that you wrote the original software. If you use this software
  17.        in a product, an acknowledgment in the product documentation would be
  18.        appreciated but is not required.
  19.     2. Altered source versions must be plainly marked as such, and must not be
  20.        misrepresented as being the original software.
  21.     3. This notice may not be removed or altered from any source distribution.
  22.  
  23.  
  24.   Brennan Underwood
  25.   brennan@nullsoft.com
  26.  
  27. */
  28.  
  29.  
  30. #include "ptrlist.h"
  31.  
  32. PtrListRoot::PtrListRoot(int initial_size) {
  33.   nitems = 0;
  34.   nslots = 0;
  35.   items = NULL;
  36.   setMinimumSize(initial_size);
  37. }
  38.  
  39. PtrListRoot::~PtrListRoot() {
  40.   removeAll();
  41. }
  42.  
  43. void PtrListRoot::copyFrom(const PtrListRoot *from) {
  44.   ASSERT(from != NULL);
  45.   int n = from->getNumItems();
  46.   removeAll();
  47.   if (n > 0) {
  48.     void **mem = from->getItemList();
  49.     ASSERT(mem != NULL);
  50.     setMinimumSize(n);
  51.     items = (void**)MEMDUP(mem, n*sizeof(void*));
  52.     nitems = n;
  53.   }
  54. }
  55.  
  56. void PtrListRoot::setMinimumSize(int _nslots) {
  57.   ASSERT(_nslots >= 0);
  58.   if (_nslots == 0 || _nslots <= nslots) return;
  59.   nslots = _nslots;
  60.   if (items)
  61.     items = (void **)REALLOC(items, sizeof(void *)*nslots);
  62.   else
  63.     items = (void **)MALLOC(sizeof(void *)*nslots);
  64. }
  65.  
  66. void *PtrListRoot::enumItem(int n) const {
  67.   if (items == NULL || n < 0 || n >= nitems) return NULL;
  68.   return items[n];
  69. }
  70.  
  71. void *PtrListRoot::addItem(void *item, int pos, int increment) {
  72.   ASSERT(increment > 0);
  73.   ASSERTPR(item != NULL, "can't put NULLs into ptrlists");
  74.   ASSERT(nitems <= nslots);
  75.  
  76.   // expand if necessary
  77.   if (items == NULL || nslots == nitems)
  78.     setMinimumSize(nslots + increment);
  79.  
  80.   items[nitems++] = item;
  81.   if (pos != POS_LAST) moveItem(nitems-1, pos);
  82.   return item;
  83. }
  84.  
  85. void **PtrListRoot::getItemList() const {
  86.   return items;
  87. }
  88.  
  89. void PtrListRoot::moveItem(int from, int to) {
  90.   if (from == to) return;
  91.   void *ptr=items[from];
  92.   if (nitems > from+1) // if moving from the end, there is nothing to shift behind our src position
  93.     //IMPORTANT note for future ports: This assumes MEMCPY accepts overlapping segments.
  94.     MEMCPY(&items[from], &items[from+1], (nitems-from)*sizeof(void *)); 
  95.   if (to > from) to--;
  96.   if (nitems > to) // if moving to the end, there is nothing to shift behind our target position
  97.     MEMCPY(&items[to+1], &items[to], (nitems-to-1)*sizeof(void *));
  98.   items[to] = ptr;
  99. }
  100.  
  101. int PtrListRoot::removeItem(void *item) {
  102.   int c = 0;
  103.  
  104.   if (item == NULL || items == NULL || nitems <= 0) return 0;
  105.  
  106.   // count occurences of item in items, remember the first one
  107.   void **p = items;
  108.   int first=-1;
  109.   for (int i=0;i<nitems;i++,p++) {
  110.     if (*p==item) {
  111.       if (c++ == 0)
  112.         first = i;
  113.     }
  114.   }
  115.  
  116.   // if we found one, remove it
  117.   if (c>0) {
  118.     removeByPos(first); // delByPos is faaast
  119.     c--; // decrease count
  120.   }
  121.  
  122.   return c; // returns how many occurences of this item left
  123. }
  124.  
  125. void PtrListRoot::removeEveryItem(void *item) {
  126.   while (removeItem(item));
  127. }
  128.  
  129. void PtrListRoot::removeByPos(int pos) {
  130.   if (pos < 0 || pos >= nitems) return;  //JC
  131.   --nitems;
  132.   if (pos == nitems) return;    // last one? nothing to copy over
  133.   MEMCPY(items+pos, items+(pos+1), sizeof(void *)*(nitems-pos));  // CT:not (nitems-(pos+1)) as nitems has been decremented earlier
  134. }
  135.  
  136. void PtrListRoot::removeLastItem() {
  137.   if (nitems == 0 || items == NULL) return;
  138.   nitems--;    // hee hee
  139. }
  140.  
  141. void PtrListRoot::removeAll() {
  142.   FREE(items); items = NULL;
  143.   nitems = 0;
  144.   nslots = 0;
  145. }
  146.  
  147. void PtrListRoot::freeAll() {
  148.   for (int i = 0; i < nitems; i++)  //JC
  149.     if (items) FREE(items[i]);
  150.   removeAll();
  151. }
  152.  
  153. int PtrListRoot::searchItem(void *item) const {
  154.   for (int i=0; i < nitems; i++)
  155.     if (items[i] == item)
  156.       return i;
  157.   return -1;
  158. }
  159.  
  160. #if 0
  161. int PtrListRoot::bsearchItem(void *item) const {
  162.   // do binary search
  163.   if (nitems == 0 || items == NULL) return -1;
  164.  
  165.   int bot = 0, top = nitems-1, mid;
  166.  
  167.   for (int c = 0; c < nitems+16; c++) {
  168.     if (bot > top) return -1;
  169.     mid = (bot + top) / 2;
  170.     if (item == items[mid]) return mid;
  171.     if (item < items[mid]) top = mid-1;
  172.     else bot = mid+1;
  173.   }
  174.   ASSERTPR(0, "binary search fucked up");
  175.   return -1;
  176. }
  177. #endif
  178.